<!--
 * @Author: your name
 * @Date: 2021-03-26 09:07:49
 * @LastEditTime: 2021-05-12 17:06:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \testc:\Users\JOJO\Desktop\vue-demo\my-app\src\components\HelloWorld.vue
-->
<template>
  <div class="login" v-loading="loading">
    <div class="note" />
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="font-size:26px">登录</span>
        <el-button
          style="float: right; padding: 3px 0"
          @click="reg"
          type="text"
        >
          注册
        </el-button>
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="auto"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            type="username"
            v-model="ruleForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item v-if="PasToggle" label="密码" prop="password">
          <el-input type="text" v-model="ruleForm.password" autocomplete="off">
            <i
              slot="suffix"
              title="隐藏密码"
              @click="changePass"
              style="cursor:pointer;"
              class="el-icon-video-pause"
            ></i>
          </el-input>
        </el-form-item>
        <el-form-item v-if="!PasToggle" label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          >
            <i
              slot="suffix"
              title="显示密码"
              @click="changePass"
              style="cursor:pointer;"
              class="el-icon-video-play"
            ></i>
          </el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="verification">
          <el-col :span="14">
            <el-input
              type="verification"
              v-model="ruleForm.verification"
              autocomplete="off"
            ></el-input>
          </el-col>
          <el-col :span="10">
            <div style="margin-left:10px">
              <ComponentUtilsVerificationIndex :value.sync="validCode" />
            </div>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">
            提交
          </el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import userApi from "../../api/user";
export default {
  data() {
    var validateUser = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      const re = /^[0-9]+.?[0-9]*$/;
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        // if (!re.test(value - 0)) {
        //   callback(new Error("密码只能是数字"));
        //   return;
        // }
        callback();
      }
    };
    var verification = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入验证码"));
      } else {
        if (
          this.ruleForm.verification.toLowerCase() ==
          this.validCode.toLowerCase()
        ) {
          callback();
        } else {
          callback(new Error("验证码错误"));
        }
      }
    };
    return {
      note: {
        backgroundImage: "url(" + require("../../assets/one.jpg") + ") ",
        backgroundPosition: "center center",
        backgroundRepeat: "no-repeat",
        backgroundSize: "cover"
      },
      PasToggle: false, //是否显示密码
      loading: false,
      validCode: "", //验证码
      ruleForm: {
        // "2019110226"  202102 2019110231
        username: this.$route.params.id ? this.$route.params.id : "",
        password: "",
        verification: ""
      },
      rules: {
        username: [{ validator: validateUser, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
        verification: [{ validator: verification, trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          this.loading = true;
          const { username, password } = this.ruleForm;

          const { code, data } = await userApi.login({
            code: username,
            password
          });
          if (code === 0) {
            this.loading = false;
            this.$message({
              message: "GNR 欢迎您 " + data.name,
              type: "success"
            });
            localStorage.setItem("sy", JSON.stringify(data));
            this.$router.push("/info");
          } else {
            this.loading = false;
            this.$message.error("错了哦");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    changePass() {
      this.PasToggle = !this.PasToggle;
    },
    reg() {
      this.$router.push("/reg");
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.login-bg {
  width: 100%;
  height: 100%;
  background: #3e3e3e;
}
.login {
  background: url("../../assets/one.jpg") no-repeat;
  background-size: 100% 100%;
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}
.clearfix {
  line-height: 1;
  vertical-align: 50%;
}
/* .clearfix:before,
.clearfix:after {
  display: table;
  content: "";
} */
.clearfix:after {
  clear: both;
}

.box-card {
  width: 400px;
}
#v_container {
  width: auto;
  height: auto;
  display: inline-flex;
  position: relative;
  top: 1rem;
  margin-top: -2rem;
}
/*body{*/
/*  width: 100%;*/
/*  !*background-color: rgb(239, 239, 239);*!*/
/*}*/
.lizi {
  position: fixed;
  top: 0;
  width: 100%;
}
</style>
